//
//  InfiniteScrollView.m
//  RBIOSBase
//
//  Created by ren on 06/12/2017.
//  Copyright © 2017 bryan. All rights reserved.
//

#import "InfiniteScrollView.h"

@interface InfiniteScrollView ()

@property (nonatomic, strong) UIImageView *firstImageView;
@property (nonatomic, strong) UIImageView *secondImageView;
@property (nonatomic, strong) UIImageView *thirdImageView;

@end

@implementation InfiniteScrollView {
	NSInteger pageNumber;
	NSInteger autoScrollNumber;
	BOOL autoScrollImageView;
}

- (void)dealloc
{
	if (curTimer) {
		[curTimer invalidate];
		curTimer = nil;
	}
}

- (instancetype)init
{
	if (self = [super init]) {
		pageNumber = 0;
		self.delegate = self;
		self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
		self.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 3, [UIScreen mainScreen].bounds.size.height);
		[self setBackgroundColor:[UIColor redColor]];
		self.pagingEnabled = YES;
		self.showsVerticalScrollIndicator = NO;
		self.showsHorizontalScrollIndicator = NO;
		self.scrollEnabled = NO;
		
	}
	return self;
}

//- (void)setImageList:(NSArray *)imageList
//{
//	_imageList = imageList;
//
//	[self setScrollStyle];
//}

#pragma mark - show image

- (void)setScrollStyle
{
	[self setScrollStyle:0 autoScroll:NO];
}

- (void)setScrollStyle:(NSInteger)curPage
{
	[self setScrollStyle:curPage autoScroll:NO];
}

- (void)setScrollStyle:(NSInteger)curPage autoScroll:(BOOL)autoScroll
{
	autoScrollNumber = 0;
	pageNumber = curPage;
	autoScrollImageView = autoScroll;
	[self addImageViews];
	if (_imageList && [_imageList count] == 1) {
		self.scrollEnabled = NO;
		[self showImageNoScroll];
	}
	else if (_imageList && [_imageList count] >= 1) {
		self.scrollEnabled = YES;
		[self showImageByNumber];
	}
	if (autoScroll) {
		[self createTimer];
	}
}

- (void)createTimer
{
	if (!curTimer) {
		curTimer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(excellent) userInfo:nil repeats:YES];
		
	}
	[[NSRunLoop mainRunLoop] addTimer:curTimer forMode:NSRunLoopCommonModes];
	
}

- (void)excellent
{
	 autoScrollNumber ++;
	if (autoScrollNumber >= 2) {
		autoScrollNumber = 2;
	}
	[self scrollRectToVisible:CGRectMake(self.bounds.size.width * autoScrollNumber, 0, self.bounds.size.width, self.bounds.size.height) animated:YES];
}

//只有一张图片的情况
- (void)showImageNoScroll
{
	[self.firstImageView setImage:[self.imageList objectAtIndex:pageNumber]];//第一张图片
}
//大于一张图片的情况
- (void)showImageByNumber
{
	[self.firstImageView setImage:[self.imageList objectAtIndex:pageNumber]];//第一张图片
	//大于总数量，指向首个，否则加1
	NSInteger number = pageNumber + 1 >= [self.imageList count]?0:pageNumber + 1;
	[self.secondImageView setImage:[self.imageList objectAtIndex:number]];//第二张
	NSInteger number1 = [self thirdPageImageView:number];
	[self.thirdImageView setImage:[self.imageList objectAtIndex:number1]];//第三张
}

- (NSInteger)thirdPageImageView:(NSInteger)number
{
	NSInteger number1 = 0;
	//如果第二个imageView中数据是imageList中的第一个对象，则下一个视图的数据从imageList的第二个对象取
	if (number == 0) {
		number1 = 1;
	}
	else {//大于总数量，指向首个，否则在pageNumber上加2
		number1 = pageNumber + 2 >= [self.imageList count]?0:pageNumber + 2;
	}
	return number1;
}

#pragma mark - add imageview

- (void)removeAllSubviews
{
	for (UIView *subview in self.subviews) {
		[subview removeFromSuperview];
	}
}

- (void)addImageViews
{
	[self removeAllSubviews];
	if (self.imageList && [self.imageList count] == 1) {
		[self addSubview:self.firstImageView];
	}
	else if (self.imageList && [self.imageList count] > 1) {
		[self addSubview:self.firstImageView];
		[self addSubview:self.secondImageView];
		[self addSubview:self.thirdImageView];
	}
}

#pragma mark - getter

- (UIImageView *)firstImageView
{
	if (!_firstImageView) {
		_firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
	}
	return _firstImageView;
}

- (UIImageView *)secondImageView
{
	if (!_secondImageView) {
		_secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
	}
	return _secondImageView;
}

- (UIImageView *)thirdImageView
{
	if (!_thirdImageView) {
		_thirdImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * 2, 0, self.bounds.size.width, self.bounds.size.height)];
	}
	return _thirdImageView;
}

#pragma mark - delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
	NSLog(@"offset------->%lf", scrollView.contentOffset.x);
	if (scrollView.contentOffset.x >= [UIScreen mainScreen].bounds.size.width * 2) {
		pageNumber ++;
		if (self.imageList && pageNumber >= [self.imageList count]) {
			pageNumber = 0;
		}
		[self scrollToSpecifyRect];
	}
	else if (scrollView.contentOffset.x <= 0) {
		pageNumber --;
		if (pageNumber < 0) {
			pageNumber = [self.imageList count] - 1;
		}
		[self scrollToSpecifyRect];
	}
}

- (void)scrollToSpecifyRect
{
	[self showImageByNumber];
	[self scrollRectToVisible:CGRectMake([UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) animated:NO];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	if (autoScrollImageView) {
		if (curTimer) {
			[curTimer invalidate];
			curTimer = nil;
		}
	}
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
	if (autoScrollImageView) {
		[self createTimer];
	}
}

@end
